package test;

import com.edu.bean.Depart;
import com.edu.bean.Emp;
import com.edu.bean.EmpVo;
import com.edu.mapper.BorrowMapper;
import com.edu.mapper.DepartMapper;
import com.edu.mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTest02 {

    SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";//路径
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
    }

    /*
     *   多对一: 利用map接受查询结果
     * */
    @Test
    public void test01(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.selectEmpById(3);
        System.out.println(emp);
        System.out.println(emp.getDepart());
    }


    @Test
    public void test02(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> empList = empMapper.selectAll();
        for (Emp emp : empList) {
//            System.out.println(emp);
//            System.out.println(emp.getDepart());
        }
        empList.get(0).getDepart();
        empList.get(1).getDepart();
        empList.get(2).getDepart();
    }

    @Test
    public void test03(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<EmpVo> empVoList = empMapper.selectEmpBySex("女");
        for (EmpVo empVo : empVoList) {
            System.out.println(empVo.getEmpId());
            System.out.println(empVo.getEmpName());
            System.out.println(empVo.getDepartName());
        }
    }

    @Test
    public void test04(){
        DepartMapper departMapper = sqlSession.getMapper(DepartMapper.class);
        Depart depart = departMapper.selectDepartAndEmpById(2);
        System.out.println(depart);
        for (Emp emp : depart.getEmpList()) {
            System.out.println(emp);
        }

    }


    @Test
    public void test05(){
        DepartMapper departMapper = sqlSession.getMapper(DepartMapper.class);
        List<Depart> departList = departMapper.selectAllDepartAndEmp();
        for (Depart depart : departList) {
            System.out.println(depart);
            for (Emp emp : depart.getEmpList()) {
                System.out.println(emp);
            }
        }
    }

    @Test
    public void test06(){
        DepartMapper departMapper = sqlSession.getMapper(DepartMapper.class);
        List<Depart> departList = departMapper.selectDepartByPhone("13551379132");
        for (Depart depart : departList) {
            System.out.println(depart);
            for (Emp emp : depart.getEmpList()) {
                System.out.println(emp);
            }
        }
    }

}
